package com.siemens.exercise;

import java.util.ArrayList;
import java.util.HashSet;

public class StringFilter {
	public static ArrayList<Integer> getNumbers(String string) throws NegtiveNumException {
		ArrayList<Integer> numbersArray = new ArrayList<Integer>();
		ArrayList<Integer> negativeArray = new ArrayList<Integer> ();
			
		if (string.equals("")) {   //return 0 if the String is empty;
			numbersArray.add(0);
			return numbersArray;
		}
			
		HashSet<String> hsSpecialChar = new HashSet<String>();
		hsSpecialChar.add("*");
		hsSpecialChar.add(".");
		hsSpecialChar.add("\\$");
		hsSpecialChar.add("^");
		hsSpecialChar.add("+");
		hsSpecialChar.add("?");
		hsSpecialChar.add("['");
		hsSpecialChar.add("']");
		hsSpecialChar.add("\\.");
			
		//handle other delimiter
		String delimiter;
		if(string.startsWith("//")){
			//replace special character with @
			for (String special : hsSpecialChar) {
				if(string.contains(special))
					string=string.replace(special, "@");
			}
			string = string.substring(2);
			delimiter = string.substring(0, string.indexOf("\n"));	
			string = string.substring(string.indexOf("\n")+1);	
				
			String[] numbers = string.split(delimiter);
			for(String strNumber : numbers) {
				Integer number = new Integer(strNumber);
				//put all the negative numbers in an arraylist
				if(number < 0) {
					negativeArray.add(number);
					continue;
				}
				//put normal numbers in an arraylist
				numbersArray.add(number);
					
				}
		}else {
				//handle string contains ",", "\n" or both
			String[] strNumbers=string.split(",");
				
			for(String str : strNumbers) {
				//string with "\n"
				String[] strTemps = str.split("\n");
				for(String str1 : strTemps){
					if(new Integer(str1)<0) {
						negativeArray.add(new Integer(str1));
						continue;
					}
					numbersArray.add(new Integer(str1));
				}
			}
		}
			
			//if the string contains negative numbers, then throw exception
		if(negativeArray.size()>0) {
			throw new NegtiveNumException(negativeArray);
		}
			return numbersArray;
	}
}
